Software Development
Unit Testing with Mocks
Unit Testing with Mocks: Creating Mocks & Verifying Behavior
Unit Testing with Mocks: Getting Started with Mockito
Unit Testing with Mocks: Mocking Exceptions & Using Spies

Unit Testing with Mocks: Creating Mocks & Verifying Behavior

Course Number:
it_jputjadj_02_enus
Lesson Objectives

Unit Testing with Mocks: Creating Mocks & Verifying Behavior

  • discover the key concepts covered in this course
  • create mocks using the @Mock annotation
  • set up multiple objects to be mocked in test cases
  • mock objects in a real-world scenario
  • use @ExtendWith to automatically initialize mocks
  • illustrate how the @InjectMock annotation works
  • use the @InjectMock annotation to inject mocks
  • use the @InjectMocks annotation to inject mocks into constructors, setters, and fields
  • verify the invocations of a method
  • use the verifyNoInteractions() function
  • illustrate the nuances of the verify() function
  • demonstrate how ordered mocks work
  • use multiple techniques to verify the behavior of objects under test and their interactions with mocks
  • summarize the key concepts covered in this course

Overview/Description
Mocks built in Mockito do more than just simulate the behavior of complex objects that your object under test depends upon. Mockito makes it easy for you to verify specific aspects of how the object under test and the mock interact. In this course, learn how to build mocks using annotations and use them to perform dependency injection into the object. Then, learn how to work with constructor, setter property, and field injection. Moving along, verify the behavior of the object under test and its interaction with the mock using Mockito constructs, such as verify(), verifyNoInteractions(), and various argument matchers. When you're done, you'll be able to use annotations to construct mocks in a clean and simple manner and test very specific aspects of the interactions between the stubs in your mocks and the object under test.

Target

Prerequisites: none

Unit Testing with Mocks: Getting Started with Mockito

Course Number:
it_jputjadj_01_enus
Lesson Objectives

Unit Testing with Mocks: Getting Started with Mockito

  • discover the key concepts covered in this course
  • recall how mocks can be used with unit tests
  • recall the benefits of using Mockito to mock objects
  • install Maven and set up a new project
  • set up IntelliJ with a Maven project
  • add dependencies for JUnit and Mockito
  • use the static mock() method to mock objects
  • use the when().thenReturn() syntax to configure behavior
  • use assertions in test cases
  • configure mocks with the when().thenReturn() syntax with input arguments
  • create mock objects of interfaces
  • sort a treeset using a comparator mock
  • set up a real-world object that can be mocked
  • mock an iterator object
  • summarize the key concepts covered in this course

Overview/Description
Mockito is a Java framework for building mocks during unit testing. When performing unit tests on an object, you often have other, complex objects that the object under test depends upon. Rather than using those complex objects, you can use Mockito to create mocks of those objects and stub their methods to simulate expected behavior. Use this course to get to grips with mocking and its relationship with unit testing. Set up a Maven project and add the required Mockito and JUnit dependencies. Create mocks of built-in classes, such as the ArrayList, and built-in interfaces, such as the Comparator. Then, work with stub methods using Mockito’s when().thenReturn() syntax. When you're done, you'll be able to identify situations where using mocks is a good idea, construct mocks, stub methods in them using Mockito, and use mock objects in unit tests.

Target

Prerequisites: none

Unit Testing with Mocks: Mocking Exceptions & Using Spies

Course Number:
it_jputjadj_03_enus
Lesson Objectives

Unit Testing with Mocks: Mocking Exceptions & Using Spies

  • discover the key concepts covered in this course
  • configure stubs to throw exceptions
  • throw exceptions from stubbed methods
  • throw exceptions from methods that return void
  • create partial mocks using spies
  • stub individual methods in spies
  • use spies with interfaces
  • verify interactions with spies
  • use the doNothing() function with void methods
  • work with argument captors
  • track input arguments to void methods using argument captors
  • summarize the key concepts covered in this course

Overview/Description
Unlike a mock, which has no real state, and in which all behavior must be stubbed, a spy is built atop a real object of the class being mocked and has almost full functionality. Only specific methods of the spy are stubbed, which is useful if it is too complex or cumbersome to mock. Use this course to learn how to identify when spies are a better choice than mocks, how to construct spies and stub specific methods, and how to raise exceptions in stubbed methods of your mocks. Then, use different methods to check how the object under test copes with those exceptions. Moving on, learn how to stub void methods and when to make use of the doNothing().when() syntax. When you're done, you'll be able to correctly use spies, mock exceptions and test their handling, mock void methods, and use argument captors.

Target

Prerequisites: none

Close Chat Live